#include "../../MyUtils/DataType/ByteArray.hpp"
#include "../../MyUtils/GLogHelper.hpp"
#include "../../MyUtils/Net/Client.hpp"
#include "../../MyUtils/Net/FdManager.hpp"
#include "../../MyUtils/Timer/Timer.hpp"
#include <iostream>
#include <thread>
using namespace std;
using MyUtils::DataType::ByteArray;

static MyUtils::MyTimer::TimerManager timer_manager;
static MyUtils::Net::FdManager fd_manager;

void read_cb( ByteArray arr )
{
    // printf( "recv %s from server\n", ( char * ) arr.data( ) );
    return;
}

int main( )
{
    MyUtils::GlogHelper helper( false );

    std::thread timer_thread( []( ) { timer_manager.run( ); } );

    /****************client*****************/

    for ( int i = 0; i < 10000; i++ )
    {
        std::string name = "client_" + std::to_string( i++ );
        int fd =
            ( MyUtils::Net::Client::connect( SOCK_STREAM, 9999, "127.0.0.1" ) );
        if ( fd == -1 )
        {
            MYLOG_ERROR( "connect error" );
        }
        else
        {
            fd_manager.add( name, fd, read_cb );
            timer_manager.addTimer( 50, true, [ name ]( ) {
                const std::string msg = "hello I'm client: " + name;
                fd_manager.send( name, msg.c_str( ), msg.length( ) );
            } );
        }
    }

    /*******************FdManager*****************/
    std::thread fd_thread( [ & ]( ) { fd_manager.run( ); } );

    fd_thread.join( );
    timer_thread.join( );
    return 0;
}